package org.idea.spring.bean.source;

import org.idea.spring.bean.source.config.MyConfig;
import org.idea.spring.bean.source.service.UserService;
import org.idea.spring.bean.source.service.UserServiceB;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author linhao
 * @Date created in 10:42 上午 2021/5/3
 */
public class SingletonBeanApplicationDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(SingletonBeanApplicationDemo.class);
        annotationConfigApplicationContext.register(MyConfig.class);
        annotationConfigApplicationContext.register(UserService.class);
        annotationConfigApplicationContext.register(UserServiceB.class);
        annotationConfigApplicationContext.refresh();
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) annotationConfigApplicationContext.getBeanFactory();
        MyConfig myConfig = (MyConfig) defaultListableBeanFactory.getBean("myConfig");
        System.out.println("myConfig is :" + myConfig);
        UserService userService = annotationConfigApplicationContext.getBean(UserService.class);
        UserServiceB userServiceB = annotationConfigApplicationContext.getBean(UserServiceB.class);
        System.out.println(userService.getMyConfig() == userServiceB.getMyConfig());
        annotationConfigApplicationContext.close();
    }
}
